/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominating.species.tos;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Filip Dinh
 */
public class EnvironmentTo implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private String description;
    private List<ElementTo> elements;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public List<ElementTo> getElements() {
        return elements;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addElement(ElementTo el) {
        if (this.elements == null) {
            elements = new ArrayList<>();
        }
        if (el == null) {
            throw new IllegalArgumentException("Element el cannot be null.");
        }
        if (el.getId() == null) {
            throw new IllegalArgumentException("Element " + el + " is not in DB - id is null.");
        }
        if (elements == null) {
            elements = new ArrayList<>();
        }
        if (this.elements.contains(el)) {
            //TODO: Do nothing - element already added
            return;
        }
        elements.add(el);
    }

    public void removeElement(ElementTo el) {
        if (el == null) {
            throw new IllegalArgumentException("Element el cannot be null.");
        }
        if (el.getId() == null) {
            throw new IllegalArgumentException("Element " + el + " is not in DB - id is null.");
        }

        if (elements == null) {
            throw new IllegalStateException("Cannot remove element from non existing list of elements.");
        }

        if (this.elements.contains(el)) {
            elements.remove(el);
        } else {
            throw new IllegalArgumentException("Given element " + el + " not found in environment's elems list.");
        }
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setElements(List<ElementTo> elements) {
        this.elements = elements;
    }

    @Override
    public String toString() {
        return "" + id + ", " + name;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final EnvironmentTo env = (EnvironmentTo) obj;
        if (this.getId() != env.getId() && (this.getId() == null || !this.getId().equals(env.getId()))) {
            return false;
        }
        return true;
    }
}
